﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using SqlHelp;
using CommonCs;
using Model;
using BugsData;

namespace DataServer
{
    public class ReplysData
    {
        public DataTable GetReplysByBugId(int size, int index, int id)
        {
            String sql = "select b.Id as uid,a.Id,a.ReplyContent,a.ReplyDate,a.ReplyUser,b.RealName from Replys as a,Users as b where a.DeleteTag='0' and a.ReplyUser=b.Id and a.BugId='" + id + "'";

            DataTable dt = new DataTable();
            try
            {
                dt = SQLHELP.getdiyerTable(size, index, sql, "Replys");
                if (dt == null || dt.Rows.Count == 0)
                {
                    return null;

                }
                else
                {
                    return dt;
                }
            }
            catch (Exception)
            {
                Common.WriteFile(CommonCs.Exceptions.Abnormal, "根据bugid获取回复信息,id:[" + id + "]异常,msg:[" + Exceptions.getreplysbybugid + "]!");
                throw new Exception(Exceptions.getreplysbybugid);
            }
        }

        public int GetReplysCount(int id)
        {
            String sql = "select COUNT(*) as num from Replys as a where a.BugId=@id";
            SqlParameter[] parm = new SqlParameter[] { 
                new SqlParameter("@id",id)
            };
            DataTable dt = new DataTable();
            try
            {
                dt = SQLHELP.GetDataTableWithParm(sql, parm);
                if (dt == null || dt.Rows.Count == 0)
                {
                    return 0;
                }
                else
                {
                    return Convert.ToInt32(dt.Rows[0]["num"]);
                }
            }
            catch (Exception)
            {
                Common.WriteFile(CommonCs.Exceptions.Abnormal, "根据bugid获取回复条数,id:[" + id + "]异常,msg:[" + Exceptions.getreplycountbybugid + "]!");
                throw new Exception(Exceptions.getreplycountbybugid);
            }
        }

        public bool InsertReply(Replys rp)
        {
            String sql = "insert into Replys(BugId,ReplyDate,ReplyUser,ReplyContent,DeleteTag) values(@BugId,@ReplyDate,@ReplyUser,@ReplyContent,0)";
            SqlParameter[] parm = new SqlParameter[] { 
                new SqlParameter("@BugId",rp.BugId),
                new SqlParameter("@ReplyDate",rp.ReplyDate),
                new SqlParameter("@ReplyUser",rp.ReplyUser),
                new SqlParameter("@ReplyContent",rp.ReplyContent)
            }; 
            int num = 0;
            int result = 0;
            BugData bd = new BugData();
            try
            {
                num = SQLHELP.GetNonQueryWithParm(sql, parm);
                if (num == 1)
                {
                    result = bd.UpdateBugReplyCount(rp.BugId);
                    if (result == 1)
                    {
                        return true;
                    }
                    else {
                        return false;
                    }
                }
                return false;
            }
            catch (Exception)
            {
                Common.WriteFile(CommonCs.Exceptions.Abnormal, "添加回复记录,id:[" + rp.BugId + "]异常,msg:[" + Exceptions.insertreply + "]!");
                throw new Exception(Exceptions.insertreply);
            }
        }
    }
}
